/*
 * MIT License
 *
 * Copyright (c) 2020 dybai
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

#include "hex.h"
#include "settty.h"

#define HEAD_SAVE        0xC0
#define HEAD_TEMP        0xC2

#define SPED_AIR_1_2K    0x00
#define SPED_AIR_2_4K    0x01
#define SPED_AIR_4_8K    0x02
#define SPED_AIR_9_6K    0x03
#define SPED_AIR_19_2K   0x04
#define SPED_AIR_50K     0x05
#define SPED_AIR_100K    0x06
#define SPED_AIR_200K    0x07

#define SPED_TTL_1200    0x00
#define SPED_TTL_2400    0x01
#define SPED_TTL_4800    0x02
#define SPED_TTL_9600    0x03
#define SPED_TTL_19200   0x04
#define SPED_TTL_38400   0x05
#define SPED_TTL_57600   0x06
#define SPED_TTL_115200  0x07

#define SPED_CHECK_8N1   0x00
#define SPED_CHECK_8O1   0x01
#define SPED_CHECK_8E1   0x02

// Communication frequency = 410MHz + (CHAN * 0.5MHz)
// Default: 0x2E: 433MHz
// Default: 433 = 410 + (0x2E * 0.5)
// CHAN = 0x00--0xC8
// freq = 410MHz--510MHz
#define CHAN_DEFAULT     0x2E

#define OPTION_TRANS_POWER_20dBm     0x00
#define OPTION_TRANS_POWER_17dBm     0x01
#define OPTION_TRANS_POWER_14dBm     0x02
#define OPTION_TRANS_POWER_10dBm     0x03

#define OPTION_MODBUS_TRANSPARENT    0x00
#define OPTION_MODBUS_FIXED_PINT     0x00

int read_param(int fd)
{
    size_t size = 0;
    unsigned char buf[3] = { 0 };

    buf[0] = 0xC1;
    buf[1] = 0xC1;
    buf[2] = 0xC1;

    size = sizeof(buf);
    if (write(fd, buf, size) < 0)
    {
        return errno;
    }

    return 0;
}

int set(int fd, unsigned char HEAD, unsigned char ADDH, unsigned char ADDL, \
    unsigned char SPED, unsigned char CHAN, unsigned char OPTION)
{
    size_t size = 0;
    unsigned char buf[6] = { 0 };

    buf[0] = HEAD;
    buf[1] = ADDH;
    buf[2] = ADDL;
    buf[3] = SPED;
    buf[4] = CHAN;
    buf[5] = OPTION;

    size = sizeof(buf);
    if (write(fd, buf, size) < 0)
    {
        return errno;
    }

    return 0;
}

int main(int argc, char *argv[])
{
    int ret = 0;
    int fd = -1;
    int value = 0;
    unsigned char speed = 0;
    unsigned char option = 0;

#if 0
    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s <HEX>\n", argv[0]);
        return 1;
    }

    if (hex_check(argv[1]))
    {
        fprintf(stderr, "ERROR: Illegal hexadecimal value: %s\n", argv[1]);
        return 1;
    }
#endif

    settty(PATH_TTY);

    fd = open(PATH_TTY, O_WRONLY);
    if (fd < 0)
    {
        perror("Open ttyUSB0");
        ret = errno;
        goto e_open;
    }

#if 0
    if ((ret = hex_conv_to_num(argv[1], &value)))
    {
        fprintf(stderr, "ERROR: Call conv_to_num() failed: %s\n", strerror(ret));
        return 1;
    }
#endif

#if 1
    speed = SPED_AIR_1_2K | SPED_TTL_9600 << 3 | SPED_CHECK_8N1 << 6;
    option = OPTION_TRANS_POWER_20dBm | OPTION_MODBUS_TRANSPARENT << 7;
    if ((ret = set(fd, HEAD_SAVE, 0, 0, speed, CHAN_DEFAULT, option)))
    {
        fprintf(stderr, "ERROR: Call set() failed: %s\n", strerror(ret));
        goto e_set;
    }
#endif

    sleep(1);
    if ((ret = read_param(fd)))
    {
        fprintf(stderr, "ERROR: Call read_param() failed: %s\n", strerror(ret));
        goto e_read_param;
    }

    printf("Done.\n");

e_read_param:
e_set:
    close(fd);
e_open:
    return ret;
}
